package com.rc.evcharger.common.base.zc;

import com.rc.evcharger.common.annotation.ByteLength;
import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.common.terminal.RecordSource;
import com.rc.evcharger.common.utils.DataUtil;
import com.rc.evcharger.common.utils.StringUtil;

import java.io.Serializable;
import java.lang.reflect.Field;

/**
 * Create by Yang on 2021/4/26
 * <p>
 * req: 充电桩上报指令
 * resp: 平台下发指令
 * 响应指令的基类
 */
public abstract class BaseZcCommand<T extends BaseZcCommand> extends RecordSource implements Serializable {


    @ByteLength(length = 7)
    public String terminalId;

    public Object getResult(String dataArea) {
        try {
            //父类赋值
            //int terminalIdLength = this.setSupperValue(dataArea);


            //int superLen = terminalIdLength * 3 - 1;
            //if (dataArea.length() > superLen) {
            //    dataArea = dataArea.substring(superLen + 1);
            //}

            Field[] fields = this.getClass().getDeclaredFields();

            for (int i = 0; i < fields.length; i++) {
                fields[i].setAccessible(true);

                ByteLength bl = fields[i].getAnnotation(ByteLength.class);
                if (bl == null) {
                    continue;
                }

                //截取数据域
                int len = bl.length() * 3 - 1;
                String value = dataArea.substring(0, len);
                if (dataArea.length() > len) {
                    dataArea = dataArea.substring(len + 1);
                }


                //反射机制赋值
                //if (bl.length() == 1 && bl.decimal() != 0) {
                if (bl.decimal() != 0) {
                    fields[i].set(this, DataUtil.getZcBDValue(value, bl.decimal(), true));
                } else if (bl.dateType().equals(SocketConstant.DATE_TYPE_ASCII)) {
                    fields[i].set(this, StringUtil.resolveVin(value));
                } else if (fields[i].getName().equals("orderNum")) {
                    fields[i].set(this, StringUtil.getZcOrderNum(value));
                } else if (fields[i].getName().equals("priceCode")) {
                    fields[i].set(this, value);
                } else if (bl.dateType().equals(SocketConstant.DATE_TYPE_BCD)) {
                    fields[i].set(this, DataUtil.getBCDValue(value, false));
                }
                //else if (fields[i].getName().endsWith("Time")) {
                //    fields[i].set(this, CP56time2aUtil.timeScale(value));
                //}
                else {
                    if (fields[i].getGenericType().toString().equals("class java.lang.String")) {
                        fields[i].set(this, DataUtil.getZcBDValue(value, bl.decimal(), true).toString());
                    } else {
                        fields[i].set(this, DataUtil.getZcBDValue(value, bl.decimal(), true));
                    }

                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return this;
    }

    public String getTerminalId() {
        return terminalId;
    }

    public void setTerminalId(String terminalId) {
        this.terminalId = terminalId;
    }

    /**
     * 获取终端ID
     *
     * @param dataArea
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    protected int setSupperValue(String dataArea) throws NoSuchFieldException, IllegalAccessException {
        //Class<?> superclass = this.getClass().getSuperclass();
        Class<?> clazz = this.getClass();
        Field terminalIdField = clazz.getField("terminalId");
        ByteLength superBl = terminalIdField.getAnnotation(ByteLength.class);
        int len = superBl.length() * 3 - 1;
        String value = dataArea.substring(0, len);
        //if (dataArea.length() > len) {
        //    dataArea = dataArea.substring(len + 1);
        //}

        terminalIdField.set(this, DataUtil.getBCDValue(value, true));

        return superBl.length();
    }

    /**
     * 对象转字符串
     *
     * @return
     * @throws IllegalAccessException
     */
    public String getObjectToString(boolean isTerminalId) throws NoSuchFieldException, IllegalAccessException {
        StringBuffer sbf = new StringBuffer();

        //获取父类桩地址
        if (isTerminalId) {
            Class<?> clazz = this.getClass();
            Field terminalIdField = clazz.getField("terminalId");
            sbf.append(terminalIdField.get(this)).append(SocketConstant.SPLIT);
        }

        //获取类本身的声明域,不含父类域
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            ByteLength bl = field.getAnnotation(ByteLength.class);
            if (bl == null) {
                continue;
            }
            //去私有化
            field.setAccessible(true);
            sbf.append(field.get(this)).append(SocketConstant.SPLIT);
        }

        return sbf.toString().trim();
    }

    public static void main(String[] args) {
        String aa = "beginTime";
        System.out.println(aa.endsWith("Time"));
    }
}
